import scrapy.commands.crawl as crawl
from scrapy.exceptions import UsageError
from scrapy.commands import ScrapyCommand


class Command(crawl.Command):

    def syntax(self):
        return "[keyword]"

    def add_options(self, parser):
        # 为命令添加选项
        ScrapyCommand.add_options(self, parser)
        parser.add_option("-k", "--keyword", type="str", dest="keyword", default="Java",
                          help="set the job you want to crawl")

    def process_options(self, args, opts):
        # 处理从命令行中传入的选项参数
        ScrapyCommand.process_options(self, args, opts)
        if opts.keyword:
            key = opts.keyword.strip()
            if key != '':
                self.settings.set('KEY', key, priority='cmdline')
        else:
            raise UsageError("U need specify the job name to crawl,use -k job name!")

    def run(self, args, opts):
        # 启动爬虫
        self.crawler_process.crawl('crawl_zhipin')
        self.crawler_process.start()